package com.iemacs.android.crt;

import java.util.List;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private SentencesDataSource  datasource;
	protected SentencesAdapter mAdapter; 
	private ListView mList; 
	protected EditText mKeywords;
	MediaPlayer mPlayer; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mList = (ListView)findViewById(R.id.list_sentences);
		mKeywords = (EditText)findViewById(R.id.keywords);
		mKeywords.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH ||
					event.getAction() == KeyEvent.ACTION_DOWN && 
					event.getAction() == KeyEvent.KEYCODE_ENTER) {


                    InputMethodManager imm = (InputMethodManager) getSystemService( INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
					
					MainActivity.this.onSearchKeywords();
					mKeywords.setText("");
					
					return true;
				}
				
				return false;
			}
		});
		datasource = new SentencesDataSource(this);
		datasource.open();

		List<Sentence> values = datasource.getAllSentences();
		mAdapter = new SentencesAdapter(this,values);
		
		this.mList.setAdapter(mAdapter);
				
	}

	public void onClick(View view) {
	
		switch (view.getId()) {
		case R.id.search:
            InputMethodManager imm = (InputMethodManager) getSystemService( INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mKeywords.getWindowToken(), 0);
            
			this.onSearchKeywords();
			break;
		}
		}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		datasource.open();
	}

	@Override
	protected void onPause() {
		super.onPause();
        if (mPlayer != null && mPlayer.isPlaying()) {
		   mPlayer.stop();
           mPlayer.release();
        }

		datasource.close();
	}

	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    
    private void onSearchKeywords() {
    	String kw = this.mKeywords.getText().toString().trim();
    	List<Sentence> values = datasource.getSentencesByKeyword(kw);
		mAdapter.clear();
		mAdapter.addAll(values);
		mAdapter.notifyDataSetChanged();
		
				
    	
    }
    
    public void PlaySound(String name) {
		int rawid = getRawId(name);
        if (mPlayer != null && mPlayer.isPlaying())
            mPlayer.stop();

		mPlayer = MediaPlayer.create(getApplicationContext(), rawid);
		mPlayer.start();
    }
    
    
    
    
    private int getRawId(String key) {
    	int id = 0;
    	key = key.substring(0, key.indexOf("."));
        int resID = getResources().getIdentifier(key, "raw", getPackageName());
       return resID;
    }
    
    
}
